<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="所在部门">
              <a-tree-select
                v-model="queryParam.deptId"
                v-decorator="['deptId', {rules: [{required: true, message: '请选择部门'}]}]"
                style="width: 100%"
                :tree-data="treeData"
                :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                placeholder="请选择"
                allow-clear
                :treeDefaultExpandedKeys="expandedKeys"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="员工姓名">
              <a-input v-model="queryParam.staffName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addStaff.add()">新建</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-staff ref="removeStaff" :staffId="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <span slot="deptId" slot-scope="text">
        <a-tag color="#108ee9">{{ text | deptTextFilter }}</a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="showInfo(record)">详情</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <remove-staff ref="removeStaff" :staffId="String(record.staffId)" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <staff-info ref="staffInfo" @ok="handleOk"></staff-info>
    <add-staff ref="addStaff" :deptsTree="treeData"   @ok="handleOk" />
    <edit-staff ref="editStaff" :deptsTree="treeData"   @ok="handleOk"></edit-staff>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '@/components'
import { queryStaff } from '@/api/basic/staff'
import { listDept, listDeptTree } from '@/api/basic/dept'
import AddStaff from './modules/staff/AddStaff'
import EditStaff from './modules/staff/EditStaff'
import RemoveStaff from './modules/staff/RemoveStaff'
import StaffInfo from './modules/staff/StaffInfo'
import { TreeSelect } from 'ant-design-vue'
// 用于处理过滤器动态获取数据
let that

export default {
  name: 'StaffList',
  components: {
    STable,
    AddStaff,
    EditStaff,
    RemoveStaff,
    StaffInfo,
    ATreeSelect: TreeSelect
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 单位对象
      depts: [],
      treeData: [],
      expandedKeys: [1],
      // 表头
      columns: [
        {
          title: '姓名',
          dataIndex: 'staffName',
          align: 'center',
          sorter: true
        },
        {
          title: '性别',
          dataIndex: 'staffSex',
          align: 'center',
          sorter: true
        },
        {
          title: '部门',
          dataIndex: 'deptId',
          scopedSlots: { customRender: 'deptId' },
          align: 'center',
          sorter: true
        },
        {
          title: '电话',
          dataIndex: 'staffTel',
          align: 'center'
        },
        {
          title: '出生日期',
          dataIndex: 'staffBirth',
          align: 'center',
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        return queryStaff(Object.assign(parameter, queryJson))
          .then(res => {
            return res.data.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {
    deptTextFilter (deptId) {
      let deptName = ''
      for (const dept of that.depts) {
        if (parseInt(dept.deptId) === parseInt(deptId)) {
          deptName = dept.deptName
          break
        }
      }
      return deptName
    }
  },
  beforeCreate () {
    that = this
  },
  created () {
    this.tableOption()
    listDept()
      .then(res => {
        if (res.data.message !== 'error') {
          this.depts = res.data.result
        } else {
          this.$message.error('操作失败！')
        }
      })
    listDeptTree()
      .then(res => {
        if (res.message !== 'error') {
          console.log(res.data.result)
          this.treeData = res.data.result
        } else {
          this.$message.error('操作失败！')
        }
      })
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleEdit (record) {
      this.$refs.editStaff.edit(record)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
      this.$message.success('操作成功！')
    },
    showInfo (record) {
      this.$refs.staffInfo.info(record)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
